<template>
  <a-form
    :model="formState"
    ref="formDataRef"
    name="basic"
    :label-col="{ style: 'width:80px' }"
    :wrapper-col="{ style: 'calc(100% - 80px)' }"
    autocomplete="off"
    @finish="onFinish"
    @finishFailed="onFinishFailed"
  >
    <a-form-item label="保险备注" name="insuranceRemark" :rules="[{ required: true, message: '请填写保险备注!' }]">
      <a-textarea v-model:value="formState.insuranceRemark" placeholder="请输入" show-count :maxlength="500" />
    </a-form-item>

    <a-form-item :wrapper-col="{ offset: 8, span: 16 }" class="submit-btn-form-item">
      <a-button :loading="loading" type="primary" html-type="submit">确定</a-button>
      <a-button @click="resetHandle">取消</a-button>
    </a-form-item>
  </a-form>
</template>

<script lang="ts" setup>
import { updateRemarkApi } from "@/api/waybillManagement/insurance/placeOrder";
import FormHook from "@/hook/formHook";
import { message } from "ant-design-vue";
import { reactive, ref, toRefs, watch } from "vue";

const props = withDefaults(
  defineProps<{
    visible: boolean;
    passData: PlaceOrder.ListRows;
  }>(),
  {}
);
const emit = defineEmits<{
  (e: "update:visible", blo: boolean): void;
  (e: "submit"): void;
}>();
const { visible, passData } = toRefs(props);
const formDataRef = ref<any>(null);
const loading = ref<boolean>(false);
const formState = reactive<Partial<{ insuranceRemark: string }>>({
  insuranceRemark: undefined
});
const { resetFields } = FormHook({ formState });

const onFinish = () => {
  const { insuranceRemark } = formState;
  const params: any = {
    id: passData.value.id,
    insuranceRemark
  };

  return updateRemarkApi(params)
    .then((res: any) => {
      const { code } = res;
      if (code === 200) {
        message.success("操作成功!");
        emit("submit");
      }
    })
    .finally(() => (loading.value = false));
};

const onFinishFailed = (errorInfo: any) => {
  console.log("Failed:", errorInfo);
};

const resetHandle = () => {
  resetFields();
  formDataRef.value.clearValidate();
  emit("update:visible", false);
};

const init = () => {
  formState.insuranceRemark = passData.value.insuranceRemark;
};
watch(
  () => visible.value,
  (v: boolean) => {
    if (!v) {
      resetFields();
      formDataRef.value.clearValidate();
    } else {
      init();
    }
  }
);
init();
</script>
<style lang="stylus" scoped>
:deep(.ant-picker){
    width: 100%;
}
</style>
